package de.baumann.browser.activitys.node

import android.app.Activity
import android.content.Context
import android.content.Intent
import de.baumann.browser.R
import de.baumann.browser.base.BaseOdinActivity
import de.baumann.browser.iview.ICreateNodeView
import de.baumann.browser.present.CreateNodePresenter
import de.baumann.browser.utils.put
import kotlinx.android.synthetic.main.activity_create_node.*
import kotlinx.android.synthetic.main.include_tool_bar.*

/**
 *@author:
 *@date:2019/9/25 16:00
 *@discription: 创建节点
 */
@Deprecated("5.0.0版本之后活动结束，不再适用")
class CreateNodeActivity : BaseOdinActivity<ICreateNodeView, CreateNodePresenter>(), ICreateNodeView {

    companion object {
        fun startCreateNodeActivity(context: Context) {
            context.startActivity(Intent(context, CreateNodeActivity::class.java))
        }

        fun startCreateNodeActivityForUpdate(context: Activity, nodeId: String, nodeName: String, nodeDesc: String, reCode: Int) {
            context.startActivityForResult(Intent(context, CreateNodeActivity::class.java).apply {
                putExtra("nodeId", nodeId)
                putExtra("nodeName", nodeName)
                putExtra("nodeDesc", nodeDesc)
            }, reCode)
        }

    }

    override fun getTitleText(): String {
        return if (getNodeId().isEmpty()) {
            "创建节点"
        } else {
            "修改节点描述"
        }
    }

    override fun getLayout(): Int {
        return R.layout.activity_create_node
    }

    override fun initView() {
        initToolbar(commToolBar,commTitle)

        btnCreateNodeDet.setOnClickListener {
            if (getNodeId().isNotEmpty()) {
                presenter.updateNodeDesc(getNodeId())
            } else {
                presenter.createNode()
            }
        }

        if (getNodeId().isNotEmpty()) {
            btnCreateNodeDet.text = "确认修改"
            edNodeName.setText(intent.getStringExtra("nodeName"))
            edNodeName.isEnabled = false
            edNodeDesc.setText(intent.getStringExtra("nodeDesc"))
        }
    }

    override fun createPresenter(): CreateNodePresenter {
        return CreateNodePresenter()
    }

    override fun createView(): ICreateNodeView {
        return this
    }

    override fun initData() {
    }


    override fun getNodeName(): String {
        return edNodeName.text.trim().toString()
    }

    override fun getNodeDesc(): String {
        return edNodeDesc.text.trim().toString()
    }

    override fun createSuccess(nodeId: String) {
        put("isJoined", true)
        NodeStatusActivity.startNodeStatusActivity(mContext, nodeId)
        finish()
    }

    private fun getNodeId(): String {
        return if(intent.getStringExtra("nodeId")==null){
            ""
        }else{
            intent.getStringExtra("nodeId")
        }
    }

    override fun updateSuccess() {
        showShortToast("修改成功")
        setResult(Activity.RESULT_OK)
        finish()
    }

}
